package zhihuilan.controlle.wechatMini;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhihuilan.common.WxMiniResponse;
import zhihuilan.common.enums.ResultEnum;
import zhihuilan.controlle.BasicController;
import zhihuilan.dto.MyFactoryProductDto;
import zhihuilan.entity.MyCategoryUnit;
import zhihuilan.entity.MyFactoryProduct;
import zhihuilan.entity.MyShop;
import zhihuilan.params.StaticParam;
import zhihuilan.params.query.MyCategoryUnitQueryParam;
import zhihuilan.params.query.MyFactoryProductQueryParam;
import zhihuilan.params.query.ProductQueryParam;
import zhihuilan.service.MyCategoryUnitService;
import zhihuilan.service.MyFactoryProductService;
import zhihuilan.service.MyShopService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/app/Factory")
public class MyFactoryController  extends BasicController {

    @Autowired
    MyShopService myShopService;

    @Autowired
    MyFactoryProductService myFactoryProductService;

    @Autowired
    MyCategoryUnitService myCategoryUnitService;
    /**
     * @api {GET} /Factory/productList 商品列表
     * @apiParam {string}  guid 登录标识
     * @apiParam {int}  type 1厂家 2市场
     * @apiParam {string}  keyword 搜索关键字
     * @apiParam {int}  page 页码
     * @apiParam {int}  page 页码
     */
    @PostMapping("/productList")
    public WxMiniResponse productList(@RequestBody ProductQueryParam queryParam){
        if (!this.isCheckLogin(queryParam.getGuid())) {
            //如果用户未登陆，提示用户未登陆信息，并且返回未登陆的枚举编码；
            return WxMiniResponse.error(ResultEnum.UNLOGIN.getKey(), ResultEnum.UNLOGIN.getValue());
        }
        MyShop myShop = myShopService.selectShopByStaffId(currentStaff.getId());
        MyFactoryProductQueryParam myFactoryProduct = new MyFactoryProductQueryParam();
        if(myShop==null){
            return WxMiniResponse.error(2013,"没有厂家");
        }else {
            myFactoryProduct.setShopid(myShop.getId());
            myFactoryProduct.setKeyword(queryParam.getKeyword());
            myFactoryProduct.setPage(queryParam.getPage());
            myFactoryProduct.setPageSize(queryParam.getPageSize());

            Map map = myFactoryProductService.selectProductByShopIdKeywordPage(myFactoryProduct);
            List<MyFactoryProduct> list = (List) map.get("data");

            if(list!=null&&list.size()>0){
                List<MyFactoryProductDto> dtoList = list.stream().map(po->{
                    MyFactoryProductDto myFactoryProductDto = new MyFactoryProductDto();
                    BeanUtils.copyProperties(po,myFactoryProductDto);
                    myFactoryProductDto.setUnitPricename(myCategoryUnitService.getCategoryUnitName(po.getUnitPrice()));
                    myFactoryProductDto.setUnitPriceQname(myCategoryUnitService.getCategoryUnitName(po.getUnitPriceQ()));
                    myFactoryProductDto.setUnitPriceZname(myCategoryUnitService.getCategoryUnitName(po.getUnitPriceZ()));
                    return myFactoryProductDto;
                }).collect(Collectors.toList());
                map.put("data",dtoList);
            }
            Map map1 = new HashMap();
            map1.put("list",map);
            List<MyCategoryUnit> units =  myCategoryUnitService.selectList(new MyCategoryUnitQueryParam());
            map1.put("unit",units);
            return WxMiniResponse.ok(map1);
        }
    }
    /***
    * @api {GET} /Factory/updateproduct 商品列表
     * @apiParam {string}  guid 登录标识
     * @apiParam {int}  type 1厂家 2市场
     * @apiParam {string}  keyword 搜索关键字
     * @apiParam {int}  page 页码
     * @apiParam {int}  page 页码
     */
    @PostMapping("/updateproduct")
    public WxMiniResponse updateproduct(@RequestBody MyFactoryProductQueryParam queryParam){
        if (!this.isCheckLogin(queryParam.getGuid())) {
            //如果用户未登陆，提示用户未登陆信息，并且返回未登陆的枚举编码；
            return WxMiniResponse.error(ResultEnum.UNLOGIN.getKey(), ResultEnum.UNLOGIN.getValue());
        }
        MyFactoryProduct myFactoryProduct = myFactoryProductService.selectById(queryParam.getId());
        if(myFactoryProduct!=null){
            MyFactoryProduct m = new MyFactoryProduct();
             BeanUtils.copyProperties(queryParam,m);
            MyFactoryProduct myFactoryProduct1 = myFactoryProductService.updateEntity(m);
            return WxMiniResponse.ok(myFactoryProduct1);
        }
        return WxMiniResponse.error(2013,"修改失败！");
    }

    /**
     * @api {GET} /Factory/delproduct 商品列表
     * @apiVersion 1.0.0
     * @apiName delproduct
     * @apiGroup Factory
     * @apiParam {string}  guid 登录标识
     * @apiParam {int}  id
     */
    @PostMapping("/delproduct")
    public WxMiniResponse delproduct(@RequestBody StaticParam staticParam){
        if (!this.isCheckLogin(staticParam.getGuid())) {
            //如果用户未登陆，提示用户未登陆信息，并且返回未登陆的枚举编码；
            return WxMiniResponse.error(ResultEnum.UNLOGIN.getKey(), ResultEnum.UNLOGIN.getValue());
        }
        MyFactoryProduct myFactoryProduct = myFactoryProductService.selectById(staticParam.getId());
        if(myFactoryProduct!=null){
            Boolean o = myFactoryProductService.deleteById(staticParam.getId());
            if(o){
                return WxMiniResponse.ok("删除成功！");
            }else {
                return WxMiniResponse.error(2013,"删除失败！");
            }

        }else{
            return WxMiniResponse.error(2013,"删除失败！");
        }
    }
}
